<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      xLine: [
        "07-01周六",
        "07-02周日",
        "07-03周一",
        "07-04周二",
        "07-05周三",
        "07-06周四",
        "07-07周五"
      ],
      xData: [
        { value: 128, name: "搜索引擎1" },
        { value: 158, name: "搜索引擎2" },
        { value: 248, name: "搜索引擎3" },
        { value: 248, name: "搜索引擎11" },
        { value: 48, name: "搜索引擎4" }
      ]
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ expectedData, actualData } = {}) {
      var that = this;
      this.chart.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross"
          },
          padding: [5, 10]
        },
        legend:{},
        tooltip:{},
        series: [
          {
            name: "订单数",
            type: "pie",
            labelLine: {
              show: false
            },
            label: {
              show: false
            },

            data: that.xData,
            animationDuration: 1000,
            animationEasing: "quadraticOut"
          }
        ]
      });
    }
  }
};
</script>
